package cn.com.cetccst.basiccollection.basicinfo;

import android.content.Context;
import android.os.Build;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import cn.com.cetccst.basiccollection.BasicInfoKey;
import cn.com.cetccst.basiccollection.InfoType;
import cn.com.cetccst.basiccollection.IBasicInfoOwner;

//AOSP能获取到的信息想都做在这里
//AOSP做不到，需要MDM接口获取的，通过指定Key用getInfo获取。
public abstract class BasicDeviceInfoOwner implements IBasicInfoOwner {

    private Context mContext;

    public BasicDeviceInfoOwner(Context context) {
        mContext = context;
    }

    //每个大类里面需要通过MDM获取的具体项
    protected abstract Object getInfoByMdm(String key, String... extras);

    /**
     * @param type 要收集的大类 {@link  InfoType}
     * @return 收集的JSON字符串
     */
    @Override
    public String collect(InfoType type) {
        switch (type) {
            case SOURCE_INFO:
                return getBasicInfo();
            case HARDWARE_INFO:
                return getHardwareInfo();

        }
        return new JSONObject().toString();
    }

    //终端基本信息
    protected String getBasicInfo() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject
                    .put("terminal_id", getInfoByMdm(BasicInfoKey.SN))
                    .put("terminal_mac", getInfoByMdm(BasicInfoKey.WLAN_DEVICE_MAC))
                    .put("terminal_seclevel", "非密")
                    .put("terminal_ip", BasicInfoUtil.getLocalIpAddress())
                    .put("terminal_name", Build.MANUFACTURER + " " + Build.MODEL)
                    .put("terminal_os", Build.VERSION.RELEASE)
                    .put("os_install_time", getInfoByMdm(BasicInfoKey.SYS_INSTALL_TIME))
                    .put("os_intergrity_status", getInfoByMdm(BasicInfoKey.SYS_INTEGRITY))
                    .put("root_status", getInfoByMdm(BasicInfoKey.ROOT_STATUS));
        } catch (JSONException e) {

        }
        return jsonObject.toString();

    }

    //终端策略信息
    protected String getRestrictions() {
        return "";
    }

    //终端应用信息
    protected String getAppsInfo() {
        List<String[]> appInfos = (List<String[]>) getInfoByMdm(BasicInfoKey.APP_INFOS);
        Map<String, String[]> usageInfo = (Map<String, String[]>) getInfoByMdm(BasicInfoKey.APP_USAGE_INFO);
        Map<String, String[]> powserUsage = (Map<String, String[]>) getInfoByMdm(BasicInfoKey.APP_POWER_USAGE);
        JSONArray jsonArray = new JSONArray();
        JSONObject result = new JSONObject();
        try {
            for (String[] appInfo : appInfos) {
                String appName = appInfo[0];
                String packageName = appInfo[1];
                String firstInstallName = appInfo[2];
                String lastUpdateTime = appInfo[3];

                String versionName = appInfo[4];
                String versionCode = appInfo[5];
                String developer = appInfo[6];

                JSONObject appJson = new JSONObject();
                appJson.put("type", BasicInfoUtil.isSystemPackage(mContext, packageName) ? "system" : "user")
                        .put("name", appName)
                        .put("package_name", packageName);

                String[] appUsage = usageInfo.get(packageName);
                appJson.put("pid", appUsage[0])
                        .put("pid", appUsage[1])
                        .put("launched_time", appUsage[3]);

                String[] appTraffics = (String[]) getInfoByMdm(BasicInfoKey.APP_TRAFFIC_INFO, packageName);
                appJson.put("data_sent_traffic", appTraffics[0])
                        .put("data_received_traffic", appTraffics[1])
                        .put("wifi_sent_traffic", appTraffics[2])
                        .put("wifi_received_traffic", appTraffics[3]);

                appJson.put("install_location", BasicInfoUtil.getInstallLocation(mContext, packageName))
                        .put("power_consumption", powserUsage.get(packageName));

                //todo 实现
                appJson.put("permissions", new JSONArray(getInfoByMdm(BasicInfoKey.APP_PERMISSION_INFO, packageName)))
                        .put("version_name", versionName)
                        .put("version_code", versionCode)
                        .put("first_install_time", firstInstallName)
                        .put("last_update_time", lastUpdateTime)
                        .put("develop", developer)
                        .put("install_type", BasicInfoUtil.isSystemPackage(mContext, packageName) ? "system" : "user");
                jsonArray.put(appJson);
            }

            result.put("apps", jsonArray)
                    .put("os_version", getInfoByMdm(BasicInfoKey.SYS_OS_VERSION))
                    .put("kernel_version", getInfoByMdm(BasicInfoKey.SYS_KERNAL_VERSION));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result.toString();
    }

    //终端应用信息
    protected String getHardwareInfo() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject
                    .put("terminal_id", getInfoByMdm(BasicInfoKey.SN));

        } catch (JSONException e) {

        }
        return jsonObject.toString();
    }

    protected String getSystemStatus() {
        return "";
    }
}
